<?php

namespace app\admin\controller;

class Member extends Common {

        /**
         * @人事管理
         */
        public function personnel() {
                $map = $this->_search("Member");
                $this->_list(D("Member"), $map, 'id', false, true);
                return $this->fetch();
        }

        /**
         * @添加 编辑员工
         * @参数param  整形integer $id 员工ID
         */
        public function personnel_add($id = "") {
                if (IS_POST) {
                        if ($id) {
                                //设置角色
                                $role = I('post.role_id');
                                $check = M('AuthGroupAccess')->where(['uid' => $id])->select();
                                if ($check == true) {
                                        M('AuthGroupAccess')->where(['uid' => $id])->save(['group_id' => $role]);
                                } else {
                                        M('AuthGroupAccess')->add(['group_id' => $role, 'uid' => $id]);
                                }
                                $this->_update("Member");
                        } else {
                                $model = M('Member');
                                $validate = \think\Loader::validate('Member');
                                if (!$validate->scene('add')->check($_REQUEST)) {
                                        return $this->iError($validate->getError());
                                }
                                $result = $model->auto('member.add')->create();
                                if (!$result) {
                                        return $this->iError($model->getError());
                                } else {
                                        $list = $model->add();
                                }
                                if ($list !== false) {
                                        //设置角色
                                        $role = I('post.role_id');
                                        $check = M('AuthGroupAccess')->where(['uid' => $list])->select();
                                        if ($check == true) {
                                                M('AuthGroupAccess')->where(['uid' => $list])->save(['group_id' => $role]);
                                        } else {
                                                M('AuthGroupAccess')->add(['group_id' => $role, 'uid' => $list]);
                                        }
                                        $this->sysAdd("执行了【" . getaction() . "】添加操作，id=" . $list);
                                        return $this->iSucess('新增成功');
                                } else {
                                        //失败提示
                                        return $this->iError('新增失败!');
                                }
                        }
                } else {
                        $departs = M('Department')->select();
                        $this->assign('departs', $departs);
                        $role = M('AuthGroup')->select();
                        $this->assign('role', $role);
                        if ($id) {
                                $member = M("Member")->field(true)->find($id);
                                $this->assign('member', $member);
                        } else {
                                $member['status'] = 1;
                                $member['sex'] = '';
                                $member['role_id'] = '';
                                $member['dept_id'] = '';
                                $this->assign("member", $member);
                        }
                        return $this->fetch();
                }
        }

        /**
         * @员工删除
         */
        public function personnel_del($id = "") {
                $this->_delete("Member", $id);
        }

        /**
         * @角色管理
         */
        public function role() {
                $this->_list(D("AuthGroup"));
                return $this->fetch();
        }

        /**
         * @角色管理-授权
         */
        public function role_set($id = '') {
                if (IS_POST) {
			if ($id) {
				$this->_update("AuthGroup",false,false);
			}
                } else {
                        if ($id) {
				$role = M("AuthGroup")->field('rules')->find($id);
                                $roles = explode(",", $role['rules']);
				$this->assign("role", $role);
                                $this->assign('roles',$roles);
			}
			$rule = M("AuthRule")->where("status=1")->order("sort desc")->select();
			$rules = array();
			foreach ($rule as $value) {
				if ($value['pid'] == 0) {
					if (!empty($rules[trim($value['group'])][$value['id']]['child'])) {
						$child = $rules[trim($value['group'])][$value['id']]['child'];
					}else{
                                                $child = array();
                                        }
					$rules[trim($value['group'])][$value['id']] = $value;
					$rules[trim($value['group'])][$value['id']]['child'] = $child;
					unset($child);
				} else {
					$rules[trim($value['group'])][$value['pid']]['child'][] = $value;
				}
			}
			$this->assign("rules", $rules);
                        return $this->fetch();
                }
        }

        /**
         * @角色管理-添加
         */
        public function role_add($id = '') {
                if (IS_POST) {
                        if ($id) {
                                $this->_update("AuthGroup", false, false);
                        } else {
                                $this->_insert("AuthGroup", false, false);
                        }
                } else {
                        if ($id) {
                                $role = M("AuthGroup")->field(true)->find($id);
                                $this->assign('role', $role);
                        } else {
                                $role['status'] = 1;
                                $this->assign("role", $role);
                        }
                        return $this->fetch();
                }
        }

        /**
         * @删除角色
         */
        public function role_del($id = '') {
                $this->_delete("AuthGroup", $id);
        }

        //部门管理
        public function department() {
                $model = D("Department");
                if (!empty($model)) {
                        $this->_list($model, array(), 'id', false, true);
                }
                return $this->fetch();
        }

        /**
         * 部门添加编辑
         */
        public function department_add($id = '') {
                if (IS_POST) {
                        if ($id) {
                                $this->_update("Department", false, false);
                        } else {
                                $this->_insert("Department", false, false);
                        }
                } else {
                        $member = M('Member')->where(['status' => 1])->select();
                        $this->assign('member', $member);
                        if ($id) {
                                $department = M("department")->field(true)->find($id);
                                $this->assign('department', $department);
                        } else {
                                $department['status'] = 1;
                                $department['leader_id'] = '';
                                $this->assign("department", $department);
                        }
                        return $this->fetch();
                }
        }

        /**
         * 部门删除
         */
        public function department_del($id) {
                $this->_delete("department", $id);
        }

}
